import os
import shutil

TARGET_NAME = "4d_systems_esp32s3_gen4_r8n16"

# 获取当前脚本所在目录
print(f'=' * 30, 'setup_tft_espi.py', '=' * 30)
print(f'Current working directory: {os.getcwd()}')
# 定义源文件和目标路径
src_header = os.path.join("assets", 'esp32_ST7789.h')
dst_header = os.path.join('.pio', 'libdeps', TARGET_NAME, 'TFT_eSPI', 'esp32_ST7789.h')

src_user_setup = os.path.join("assets", 'User_Setup_Select.h')
dst_user_setup = os.path.join('.pio', 'libdeps', TARGET_NAME, 'TFT_eSPI', 'User_Setup_Select.h')

src_lvgl_conf= os.path.join("assets", 'lv_conf.h')
dst_lvgl_conf = os.path.join('.pio', 'libdeps', TARGET_NAME, 'lv_conf.h')

# 强制复制函数
def force_copy(src, dst):
    if os.path.exists(dst):
        print(f'Removing existing file: {dst}')
        os.remove(dst)
    print(f'Copying {src} to {dst}')
    shutil.copy(src, dst)

# 强制复制函数
def ignore_if_exists_copy_file(src, dst):
    if os.path.exists(dst):
        print(f'Ignoring existing file: {dst}')
        return # 如果目标文件存在，则忽略
    print(f'Copying {src} to {dst}')
    shutil.copy(src, dst)

# 执行复制
force_copy(src_header, dst_header)
force_copy(src_user_setup, dst_user_setup)
ignore_if_exists_copy_file(src_lvgl_conf, dst_lvgl_conf)

# 还需要将lvgl目录中的demos文件夹复制到lvgl中的src目录下
src_demos = os.path.join('.pio', 'libdeps', TARGET_NAME, 'lvgl', 'demos')
dst_demos = os.path.join('.pio', 'libdeps', TARGET_NAME, 'lvgl', 'src', 'demos')
# 如果监测到目标目录已经存在了，就无视
def ignore_if_exists_else_copy(src, dst):
    if os.path.exists(dst):
        print(f'Ignoring existing directory: {dst}')
    else:
        print(f'Copying {src} to {dst}')
        shutil.copytree(src, dst)

ignore_if_exists_else_copy(src_demos, dst_demos)
print(f'=' * 30, 'setup_tft_espi.py', '=' * 30)